package org.finmanager.core;

import com.alee.extended.date.WebDateField;
import com.alee.extended.layout.TableLayout;
import com.alee.laf.panel.WebPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.finmanager.core.Constants.ITEM_INPUT_UI_HEIGHT;
import static org.finmanager.core.Constants.ITEM_INPUT_UI_NAME;
import static org.finmanager.core.Constants.ITEM_INPUT_UI_WIDTH;
import static org.finmanager.core.Constants.ITEM_INPUT_UI_RESIZABLE;
import static org.finmanager.core.Constants.DATE_FORMAT;

/**
 * Created by Kelvin on 21/3/2014.
 *
 * In the first Budget function, there is a "+" button on the right top corner to add items
 * And if user right click a row on in the table, he may edit or delete items
 * All are by popping a small window 
 * This class tells how users add/edit an Item under this window
 * Components are added to a frame
 *
 * TODO: Exception Handling
 */
public abstract class AbstractItemInputUI {
	private JPanel mainPanel = new JPanel();

	private JFrame frame;

	// Input fields
	//1.Name
	//2.Date using a calendar(e.g. able to click a date on a calendar instead of inputting date/month/year
	//3.Amount
	//4.Category using COMBOBOX
	private JTextField nameTextField;
	private WebDateField dateField;
	private JTextField amountTextField;
	private JComboBox categoryComboBox;

	private JButton okayButton;

	// This constructor is called when adding an item
	// A category can be specified before
	public AbstractItemInputUI(ItemManagerController itemManagerController, ComboBoxModel listModel) {
		createAndShowGUI(itemManagerController.getUIMainComponent(), listModel);

		okayButton.addActionListener(itemManagerController);
		okayButton.addActionListener(new CloseWindowListener());
	}

	protected JComponent getMainComponent() {
		return mainPanel;
	}

	private void createAndShowGUI(JComponent relative, ComboBoxModel listModel) {
		WebPanel inputPanel = new WebPanel();

		nameTextField       = new JTextField("", 10);
		dateField           = new WebDateField(new Date());
		amountTextField     = new JTextField("", 10);
		categoryComboBox    = createComboBox(listModel);

		TableLayout tableLayout = new TableLayout(new double[][]{{TableLayout.PREFERRED, TableLayout.FILL},
				{TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}, 10, 3);
		inputPanel.setLayout(tableLayout);
		inputPanel.setMargin(15, 30, 15, 30);

		inputPanel.add(new JLabel("Name", JLabel.TRAILING), "0, 0");
		inputPanel.add(nameTextField, "1, 0");
		inputPanel.add(new JLabel("Date", JLabel.TRAILING), "0, 1");
		inputPanel.add(dateField, "1, 1");
		inputPanel.add(new JLabel("Amount", JLabel.TRAILING), "0, 2");
		inputPanel.add(amountTextField, "1, 2");
		inputPanel.add(new JLabel("Category", JLabel.TRAILING), "0, 3");
		inputPanel.add(categoryComboBox, "1, 3");

		dateField.setDateFormat(new SimpleDateFormat(DATE_FORMAT));

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

		okayButton = new JButton("OK");
		JButton cancelButton = new JButton("Cancel");



		cancelButton.addActionListener(new CloseWindowListener());

		buttonPanel.add(okayButton);
		buttonPanel.add(cancelButton);

		getMainComponent().setLayout(new BorderLayout(0, 0));
		getMainComponent().add(inputPanel, BorderLayout.CENTER);
		getMainComponent().add(buttonPanel, BorderLayout.SOUTH);



		frame = new JFrame(ITEM_INPUT_UI_NAME);
		frame.setSize(new Dimension(ITEM_INPUT_UI_WIDTH, ITEM_INPUT_UI_HEIGHT));
		frame.setLocationRelativeTo(relative);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setResizable(ITEM_INPUT_UI_RESIZABLE);

		frame.add(getMainComponent());

		frame.setVisible(true);
	}

	// Creating the combo box containing categories
	// Categories are loaded from the CategoryList
	private JComboBox createComboBox(ComboBoxModel listModel) {
		JComboBox comboBox = new JComboBox(listModel);

		comboBox.setSelectedIndex(0);

		return comboBox;
	}

	public Item getItemFromInput() {
		String name             = nameTextField.getText();
		int year                = dateField.getDate().getYear() + 1900;
		int month               = dateField.getDate().getMonth();
		int dayOfMonth          = dateField.getDate().getDate();
		Double amount           = Double.parseDouble(amountTextField.getText());
		Category category       = (Category) categoryComboBox.getSelectedItem();

		return new Item(name, year, month, dayOfMonth, amount, category);
	}

	protected void setFieldFromItem(Item item) {
		DecimalFormat df = new DecimalFormat("#.#");

		nameTextField.setText(item.getName());
		dateField.setDate(new Date(item.getYear() - 1900, item.getMonth(), item.getDayOfMonth()));
		amountTextField.setText(df.format(item.getAmount()));
		categoryComboBox.setSelectedItem(item.getCategory());
	}

	protected JButton getOkayButton() {
		return okayButton;
	}

	protected JComboBox getCategoryComboBox() {
		return categoryComboBox;
	}

	private class CloseWindowListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			frame.dispose();
		}
	}
}
